package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.game.GameConfigVo;
import com.qumitech.visualnovel.controller.vo.voiceRoom.VoiceRoomVo;
import com.qumitech.visualnovel.pubdef.dto.advent.AdventAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.advent.AdventAdminSaveDTO;
import com.qumitech.visualnovel.pubdef.dto.advent.AdventAdminUpdateDTO;
import com.qumitech.visualnovel.service.advent.AdventConfigService;
import com.qumitech.visualnovel.service.oldgame.OldGameConfigService;
import com.qumitech.visualnovel.service.voiceRoom.VoiceRoomConfigService;
import com.google.common.collect.ImmutableMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Auther: yilu
 * @Date: 2018/11/22 14:59
 * @Description:
 */

@RestController
@RequestMapping("/api/product_config")
public class ProductConfigController {
    @Autowired
    private AdventConfigService adventConfigService;
    @Autowired
    private VoiceRoomConfigService voiceRoomConfigService;
    @Autowired
    private OldGameConfigService gameConfigService;

    @RequiresPermissions("banner-settings:view")
    @RequestMapping(value = "/advent", method = RequestMethod.GET)
    public BaseRsp getAdvents(AdventAdminQueryDTO queryDTO) {
        return adventConfigService.getAdvents(queryDTO);
    }

    @PostMapping("/advent")
    public BaseRsp addAdvent(@RequestBody AdventAdminSaveDTO saveDTO) {
        return adventConfigService.addAdvent(saveDTO);
    }

    @PutMapping("/advent/{cfgId}")
    public BaseRsp updateAdvent(@PathVariable("cfgId") String cfgId, @RequestBody AdventAdminUpdateDTO updateDTO) {
        updateDTO.setCfgId(cfgId);
        return adventConfigService.updateAdvent(updateDTO);
    }


    @DeleteMapping("/advent/{cfgId}")
    public BaseRsp deleteAdvent(@PathVariable("cfgId") String cfgId) {
        return adventConfigService.rmAdvent(cfgId);
    }


    @PutMapping("/voiceroom")
    public BaseRsp updateVoiceRoomCfg(@RequestBody VoiceRoomVo voiceRoomVo) {
        voiceRoomConfigService.updateVoiceRoomCfg(voiceRoomVo);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/voiceroom")
    public BaseRsp getVoiceRoom(@RequestParam(value = "room_type", defaultValue = "10") Byte roomType,
                                @RequestParam(value = "order_by", defaultValue = "1") Byte orderBy,
                                @RequestParam(value = "offset", defaultValue = "0") Integer offset,
                                @RequestParam(value = "limit", defaultValue = "20") Integer limit) {
        Map<String, Object> voiceRoomRsp = voiceRoomConfigService.getVoiceRoom(roomType, orderBy, offset, limit);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("voicerooms", voiceRoomRsp.get("room_list"), "count", voiceRoomRsp.get("count")));
    }

    @GetMapping("/game")
    public BaseRsp getGames(@RequestParam(value = "offset", defaultValue = "0") Integer offset,
                            @RequestParam(value = "limit", defaultValue = "20") Integer limit,
                            @RequestParam(name = "game_id", required = false) String gameId,
                            @RequestParam(name = "game_name", required = false) String gameName,
                            @RequestParam(name = "app_version", required = false) String appVersion,
                            @RequestParam(name = "app_channel", required = false) String appChannel) {
        return new BaseRsp(ResultEnum.SUCCESS, gameConfigService.getGames(gameId, gameName, appChannel, appVersion, offset, limit));
    }

    @PutMapping("/game")
    public BaseRsp updateGame(@RequestBody GameConfigVo gameConfigVo) {
        gameConfigService.updateGame(gameConfigVo);
        return new BaseRsp(ResultEnum.SUCCESS);
    }
}
